	<?php
	print '<script type="text/javascript">
			<!--
			function confirmation() {
				var answer = confirm("Are you sure you want to delete this language?")
				if (answer) {
				window.location = "?page='.$_GET['page'].'&language='.substr($this->options['language'], 0,strrpos($this->options['language'],'.')).'&do=delete";
				} else {
				return false;
				}
			}
	//-->
	</script>';
	
	if ($_GET['language'] != '' && $_GET['do'] == 'edit') {
	$lines = file(TEMPLATEPATH.'/languages/'.$this->options['language']);
	$matches = array();
	$lang = array();
	foreach($lines as $line) {
		if (preg_match('/DEFINE\(\'([^\']*)\',\s*\'([^\\\\\']*(?:\\.[^\\\\\']*)*)\'\);/i', $line, $matches)) {
		$lang[$matches[1]] = $matches[2];
		}
	}
	}
	
	if ($_GET['language'] != '' && $_GET['do'] == 'edit' && $_POST['save_l'] == 'Update Language') {
		if ($file = fopen(TEMPLATEPATH.'/languages/'.$this->options['language'], 'w')) {
			fwrite($file, "<?php\r\n");
			foreach($lang as $key => $value) {
			$lang[$key] = $_POST[$key];
			$updated_line = "DEFINE('".$key."', '".$_POST[$key]."');";
			fwrite($file, "$updated_line\r\n");
			}
			fwrite($file, "?>");
		}
		fclose($file);
	}

	if ($_GET['language'] != '' && $_GET['do'] == 'new') {
	$lines = file(TEMPLATEPATH.'/languages/'.$this->options['language']);
	$matches = array();
	$lang = array();
	foreach($lines as $line) {
		if (preg_match('/DEFINE\(\'([^\']*)\',\s*\'([^\\\\\']*(?:\\.[^\\\\\']*)*)\'\);/i', $line, $matches)) {
		$lang[$matches[1]] = $matches[2];
		}
	}
	}
	
	if ($_GET['language'] != '' && $_GET['do'] == 'new' && $_POST['new_l'] == 'Create New Language') {
		if (!empty($_POST['new_language'])) {
			if (!file_exists(TEMPLATEPATH.'/languages/'.$_POST['new_language'].'.php')) {
				if ($file = fopen(TEMPLATEPATH.'/languages/'.$_POST['new_language'].'.php', 'w')) {
					fwrite($file, "<?php\r\n");
					foreach($lang as $key => $value) {
					$updated_line = "DEFINE('".$key."', '".$_POST[$key]."');";
					fwrite($file, "$updated_line\r\n");
					}
					fwrite($file, "?>");
				}
				fclose($file);
			}
		}
	}

	if ($_POST['ss_action'] != 'save' && $_GET['language'] != '' && $_GET['do'] == 'delete') {
		$dirPath = TEMPLATEPATH. '/languages/';
		if ($handle = opendir($dirPath)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != "..") {
					if (is_file("$dirPath/$file")) {
						$langfiles[] = $file;
					}
				}
			}
		closedir($handle);
		}
		if (!file_exists($dirPath.$this->options['language'])) {
		$msg_delete_lang = '<p style="background:red;display:inline;color:#fff;font-weight:bold;padding: 1px;">This language does not exist or it might have been deleted already!</p>';
		} elseif ($_GET['language'] == remove_ext($this->default_settings['language'])) {
		$msg_delete_lang = '<p style="background:red;display:inline;color:#fff;font-weight:bold;padding: 1px;">You cannot delete the default theme language!</p>';
		} else {
		$msg_delete_lang = '<p style="background:green;display:inline;color:#fff;font-weight:bold;padding: 1px;">Language deleted successfully. Please save changes now!</p>';
		unlink($dirPath.$this->options['language']);
		}
	}
	?>
	
	<div id="12" class="cptab">
	<h2>Language Manager</h2>
	<p>Language files are stored in <b>languages</b> directory. You can add, edit, or delete languages from this panel.</p>
	
	<fieldset><legend>Default Language</legend>
	<label for="cp_language">Choose from available languages</label>
	<select name="cp_language" class="widefat big" onchange="this.form.submit();">
	<?php
	$dirPath = TEMPLATEPATH. '/languages/';
	if ($handle = opendir($dirPath)) {
	while (false !== ($file = readdir($handle))) {
	if ($file != "." && $file != "..") {
		if (is_file("$dirPath/$file")) {
		?>
	<option value="<?php echo $file; ?>"<?php selected($file, $this->options["language"]); ?>><?php echo ucfirst(remove_ext($file)); ?></option>
		<?php
		}
	}
	}
	closedir($handle);
	}
	?>
	</select><div class="icons-c"><a href="?page=<?php echo $_GET['page']; ?>&amp;language=<?php echo remove_ext($this->options['language']); ?>&amp;do=new" title="Create a New Language"><img src="<?php bloginfo('template_directory'); ?>/cp/img/icon_new.png" alt="Create a New Language" /></a><a href="?page=<?php echo $_GET['page']; ?>&amp;language=<?php echo remove_ext($this->options['language']); ?>&amp;do=edit" title="Edit Language"><img src="<?php bloginfo('template_directory'); ?>/cp/img/icon_edit.gif" alt="Edit Language" /></a><a href="#" title="Delete Language" onclick="javascript:confirmation();"><img src="<?php bloginfo('template_directory'); ?>/cp/img/icon_delete.gif" alt="Delete Language" /></a></div>
	<div class="clear"></div>
	</fieldset>
	
	<?php if ($_GET['language'] != '' && $_GET['do'] == 'edit') : ?>
	<fieldset><legend>Edit Language - <?php echo ucfirst(remove_ext($this->options['language'])); ?></legend>
	<?php foreach ($lang as $key => $value) { ?>
	<label for="<?php echo $key; ?>"><?php echo $key; ?></label>
	<input class="widefat large" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo stripslashes($value); ?>">
	<div class="clear"></div>
	<?php } ?>
	</fieldset>
	<input type="submit" value="Update Language" name="save_l" class="button" /><div class="clear"></div>
	<?php endif; ?>
	
	<?php if ($_GET['language'] != '' && $_GET['do'] == 'new') : ?>
	<fieldset><legend>Create a New Language</legend>
	<label for="new_language">LANGUAGE_UNIQUE_NAME</label>
	<input class="widefat large" name="new_language" id="new_language" value="LANGUAGE_NAME">
	<div class="clear"></div>
	<?php foreach ($lang as $key => $value) { ?>
	<label for="<?php echo $key; ?>"><?php echo $key; ?></label>
	<input class="widefat large" name="<?php echo $key; ?>" id="<?php echo $key; ?>" value="<?php echo stripslashes($value); ?>">
	<div class="clear"></div>
	<?php } ?>
	</fieldset>
	<input type="submit" value="Create New Language" name="new_l" class="button" /><div class="clear"></div>
	<?php endif; ?>
	
	<?php if ($_GET['language'] != '' && $_GET['do'] == 'delete') : ?>
	<p><?php print $msg_delete_lang; ?></p>
	<?php endif; ?>
	
	</div>